﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;

namespace ef.DataBase
{
    /// <summary>
    /// 数据库上下文配置
    /// </summary>
    internal class DataConfig
    {

        /// <summary>
        /// 得到数据连接串
        /// </summary>
        /// <returns></returns>
        internal static string GetConnectionString()
        {
            string conn;
            if (System.Diagnostics.Debugger.IsAttached)
            {
                conn = VConfigs.WebInfoConfig.Get_Config().DevsSqlConn;
            }
            else
            {
                conn = VConfigs.WebInfoConfig.Get_Config().ReleaseSqlConn;
            }
            return conn;
        }
        /// <summary>
        /// 根据传入的类型，进行DBContext出始化
        /// </summary>
        /// <param name="InitType"></param>
        internal static void SetInitializer(InitializerTypes InitType)
        {
            switch (InitType)
            {
                case InitializerTypes.Standard:
                    Database.SetInitializer(new CreateDatabaseIfNotExists<DataContext>());
                    break;
                case InitializerTypes.ReCreateAlways:
                    Database.SetInitializer(new DropCreateDatabaseAlways<DataContext>());
                    break;
                case InitializerTypes.ReCreateByChange:
                    Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DataContext>());
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// DBContext初始化方法
        /// </summary>
        internal static void Init()
        {
            Database.DefaultConnectionFactory = new SqlConnectionFactory();
            if (System.Diagnostics.Debugger.IsAttached)
            {
                SetInitializer(InitializerTypes.ReCreateByChange);
            }
            else
            {
                SetInitializer(InitializerTypes.Standard);
            }
        }



    }
}
